package cn.jsl.product.controller;

import cn.jsl.basic.util.AjaxResult;
import cn.jsl.basic.util.PageList;
import cn.jsl.product.domain.Product;
import cn.jsl.product.query.ProductQuery;
import cn.jsl.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    public IProductService productService;


    /**
     * 保存和修改公用的
     *
     * @param product 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product) {
        try {
            if (product.getId() != null)
                productService.update(product);
            else
                productService.save(product);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            productService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Product get(@PathVariable("id") Long id) {
        return productService.loadById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Product> list() {

        return productService.loadAll();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            productService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }

    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Product> json(@RequestBody ProductQuery query) {
        return productService.queryPage(query);
    }

    /**
     * 上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody HashSet<Long> ids) {
        return productService.onsale(ids);
    }

    /**
     * 下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody HashSet<Long> ids) {
        return productService.offsale(ids);
    }

    //传输图片ID
    @GetMapping("/detail/{id}")
    public Product loadById(@PathVariable ("id") Long id) {
        return productService.loadById(id);
    }

}
